package com.sharp.oss.controller;

import com.sharp.kernel.config.response.Response;
import com.sharp.kernel.config.response.ResponseHandler;
import com.sharp.kernel.constant.err.ErrCodeSys;
import com.sharp.framework.service.cache.FrameCacheService;
import com.sharp.kernel.utils.BeanUtil;
import com.sharp.framework.utils.DictEnumUtil;
import com.sharp.kernel.validate.Validator;
import com.sharp.kernel.vo.common.CommonResVO;
import com.sharp.kernel.vo.query.QueryRespVO;
import com.sharp.oss.constant.ObjectStorageConst;
import com.sharp.oss.constant.dict.ObjectStorageTypeDict;
import com.sharp.oss.constant.param.ObjectStorageParamConst;
import com.sharp.oss.service.ObjectStorageService;
import com.sharp.oss.vo.UploadVO;
import com.sharp.oss.vo.oss.GetOssFileInfo;
import com.sharp.oss.vo.oss.QueryOssReq;
import com.sharp.oss.vo.oss.QueryOssRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Title: ObjectController
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2019/10/15 22:24
 * @description Project Name: Grote
 * @Package: com.srct.service.controller
 */
@Api(value = "上传文件", tags = "上传文件")
@RestController("ObjectController")
@RequestMapping(value = "/oss")
public class ObjectController {

    private static final String PREFIX_SERVICE_NAME = "objectStorageService";
    private static final String SUFFIX_SERVICE_NAME = "Impl";

    @Resource
    private HttpServletRequest request;
    @Resource
    private FrameCacheService frameCacheService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "fileName", value = "文件名", required =
                    true)})
    public ResponseEntity<Response<QueryOssRes>> get(GetOssFileInfo req) {
        String ossType = frameCacheService.getParamValueAvail(ObjectStorageParamConst.OSS_TYPE);
        ObjectStorageService oss =
                (ObjectStorageService) BeanUtil.getBean(PREFIX_SERVICE_NAME + ossType + SUFFIX_SERVICE_NAME);
        Validator.assertNotNull(oss, ErrCodeSys.SYS_CONFIG_NOT_EXIST, "上传类型");
        Validator.assertNotBlank(req.getFileName(), ErrCodeSys.PA_DATA_NOT_EXIST, "文件名");
        QueryOssRes res = oss.get(req);
        return ResponseHandler.generateResponse(res);
    }

    @RequestMapping(value = "", method = {RequestMethod.PUT, RequestMethod.POST})
    public ResponseEntity<Response<UploadVO>> upload(MultipartFile file,
                                                     @RequestParam(value = "type", required = false) String type,
                                                     @RequestParam(value = "fileName", required = false) String fileName) {
        String ossType = frameCacheService.getParamValueAvail(ObjectStorageParamConst.OSS_TYPE);
        ObjectStorageService oss =
                (ObjectStorageService) BeanUtil.getBean(PREFIX_SERVICE_NAME + ossType + SUFFIX_SERVICE_NAME);
        if (StringUtils.isNotBlank(fileName)) {
            if (StringUtils.startsWith(fileName, ObjectStorageConst.SEP)) {
                fileName = fileName.substring(1);
            }
            if (StringUtils.endsWith(fileName, ObjectStorageConst.SEP)) {
                fileName = fileName.substring(0, fileName.length() - 2);
            }
        }
        if (StringUtils.isNotBlank(type)) {
            if (StringUtils.startsWith(type, ObjectStorageConst.SEP)) {
                type = type.substring(1);
            }
            if (StringUtils.endsWith(type, ObjectStorageConst.SEP)) {
                type = type.substring(0, type.length() - 2);
            }
        }
        try {
            DictEnumUtil.getEnumByValue(type, ObjectStorageTypeDict.class);
        } catch (Exception e) {
            type = ObjectStorageTypeDict.OTHER.getValue();
        }
        Validator.assertNotNull(oss, ErrCodeSys.SYS_CONFIG_NOT_EXIST, "上传类型");
        UploadVO res = oss.upload(request, file, fileName, type);
        return ResponseHandler.generateResponse(res);
    }

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public ResponseEntity<Response<QueryRespVO<QueryOssRes>>> query(@RequestBody QueryOssReq req) {
        String ossType = frameCacheService.getParamValueAvail(ObjectStorageParamConst.OSS_TYPE);
        ObjectStorageService oss =
                (ObjectStorageService) BeanUtil.getBean(PREFIX_SERVICE_NAME + ossType + SUFFIX_SERVICE_NAME);
        Validator.assertNotNull(oss, ErrCodeSys.SYS_CONFIG_NOT_EXIST, "上传类型");
        QueryRespVO<QueryOssRes> res = oss.query(req);
        return ResponseHandler.generateResponse(res);
    }

    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResponseEntity<Response<CommonResVO>> query(Long id) {
        String ossType = frameCacheService.getParamValueAvail(ObjectStorageParamConst.OSS_TYPE);
        ObjectStorageService oss =
                (ObjectStorageService) BeanUtil.getBean(PREFIX_SERVICE_NAME + ossType + SUFFIX_SERVICE_NAME);
        Validator.assertNotNull(oss, ErrCodeSys.SYS_CONFIG_NOT_EXIST, "上传类型");
        oss.delete(id);
        return ResponseHandler.commonResponse();
    }

}
